package com.huaxindata.im.model.entity;

import android.os.Parcel;
import android.os.Parcelable;

import com.huaxindata.im.main.chat.Helper.ContactHelper;
import com.huaxindata.im.main.manager.ContactManager;
import com.huaxindata.im.model.contact.Contact;

/**
 * @author songxf
 * @date 2018/1/8
 * @Desc $desc$
 * Copyright HUAXIN DATA. All rights reserved.
 */
public class ChatMessage implements Parcelable {
    //消息拼接符
    public final static String PREX          = "#&";
    //位置消息拼接符
    public final static String PREX_LOCATION = "#";

    public static class Status {
        //  public static final int IDEL = 3;//初始状态
        public static final int DOING   = 0;//发送中的状态
        public static final int ERROR   = 1;//消息收、发失败
        public static final int SUCCESS = 2;//消息收、发处理成功
    }

    public String          sender;//发送人
    public String          receiver;//接收人
//    public String          messageId;//消息id
//    public String          sessionId;//会话ID
    public ChatMessageBody messageBody;//消息体列表
    public long            timestamp;//消息发送或接收的时间(发送消息是本地时间，接收消息是服务器时间)
    public boolean         isGroup;//此消息是否是群聊消息
    public int             messageState;//发送状态
    public int         read;//是否读取过
    public String          senderName;//群聊消息里的发送者用户名
    public int             type;//消息类型
    public String          message;

    public String simpleInfo;

    public ChatMessage() {
    }

    public ChatMessage(int type) {
        this.type=type;
    }

    public ChatMessage(String sender, String receiver, int type) {
        this.sender=sender;
        this.receiver=receiver;
        this.type=type;
    }

    public ChatMessage(int type, String sender, String receiver, long timestamp) {
        this.sender=sender;
        this.receiver=receiver;
        this.type=type;
        this.timestamp=timestamp;
    }

    public ChatMessage(int type, String message) {
        this.type=type;
        this.message=message;
    }

    public ChatMessageBody getMessageBody() {
        return messageBody;
    }

    /**
     *  发送人 + 接收人 + 消息内容 字节长度
     * @param content
     * @return
     */
    public int getByteLength(String content) {
        return  getAllMessage(content).getBytes().length;
    }

    public String getAllMessage(String  content) {
        return sender + receiver + content;
    }

    /**
     * 本消息是否是某指定id发送的
     * @param id
     * @return
     */
    public boolean isSender(String id) {
        if (id==null)
            return false;
        return id.equals(sender);
    }

    /**
     * 主要用于在聊天记录上显示最近的一条记录，语音则显示”语音”
     * @return
     */
    public CharSequence getSimpleString() {
        if (simpleInfo != null) return simpleInfo;
        final StringBuilder sb = new StringBuilder();
        //提示信息&群消息
        if (receiver != null && ContactHelper.isGroup(receiver) && (type != ChatMessageBody.Type.SYSTEM)) {
            Contact contact = ContactManager.getGroupById(receiver);
            if (contact!=null){
                sb.append(contact.nickName).append(":");
            }
        }
        if (type != ChatSysMessageBody.Type.TEXT) {
            switch (type) {
                case ChatSysMessageBody.Type.SYSTEM:
                    ChatSysMessageBody chatSysMessageBody= (ChatSysMessageBody) getMessageBody();
                    sb.append(chatSysMessageBody.getSimpleString(chatSysMessageBody.content));
                    break;
                case ChatSysMessageBody.Type.IMAGE:
                    sb.append("[图片]");
                    break;
                case ChatSysMessageBody.Type.VIDEO:
                    sb.append("[视频]");
                    break;
                case ChatSysMessageBody.Type.VOICE:
                    sb.append("[语音]");
                    break;
                case ChatSysMessageBody.Type.FILE:
                    sb.append("[文件]");
                    break;
                case ChatSysMessageBody.Type.LOCATION:
                    sb.append("[位置]");
                    break;
                case ChatSysMessageBody.Type.SVG:
                    sb.append("[工艺图]");
                    break;
                case ChatSysMessageBody.Type.STATION:
                    sb.append("[站点]");
                    break;
                case ChatSysMessageBody.Type.TARGET:
                    sb.append("[指标]");
                    break;
                case ChatSysMessageBody.Type.WORKTASK:
                    sb.append("[工单]");
                case ChatSysMessageBody.Type.WARN:
                    sb.append("[告警]");
                    break;
            }
            return simpleInfo = sb.toString();
        } else {
            return simpleInfo =this.message;
        }
    }

    public ChatMessage(Parcel in) {
        sender = in.readString();
        receiver = in.readString();
        timestamp = in.readLong();
        isGroup = in.readByte() != 0;
        messageState = in.readInt();
        read = in.readInt();
        senderName = in.readString();
        type = in.readInt();
        message = in.readString();
        simpleInfo = in.readString();
    }

    public static final Creator<ChatMessage> CREATOR = new Creator<ChatMessage>() {
        @Override
        public ChatMessage createFromParcel(Parcel in) {
            return new ChatMessage(in);
        }

        @Override
        public ChatMessage[] newArray(int size) {
            return new ChatMessage[size];
        }
    };

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeString(sender);
        parcel.writeString(receiver);
        parcel.writeLong(timestamp);
        parcel.writeByte((byte) (isGroup ? 1 : 0));
        parcel.writeInt(messageState);
        parcel.writeInt(read);
        parcel.writeString(senderName);
        parcel.writeInt(type);
        parcel.writeString(message);
        parcel.writeString(simpleInfo);
    }

    @Override
    public String toString() {
        return "ChatMessage{" +
                "sender='" + sender + '\'' +
                ", receiver='" + receiver + '\'' +
                ", messageBody=" + messageBody +
                ", timestamp=" + timestamp +
                ", isGroup=" + isGroup +
                ", messageState=" + messageState +
                ", read=" + read +
                ", senderName='" + senderName + '\'' +
                ", type=" + type +
                ", message='" + message + '\'' +
                ", simpleInfo='" + simpleInfo + '\'' +
                '}';
    }
}
